from flask import (Blueprint, request)
from json import (dumps)
from msg import condition
from mylog import LOG
from service.statistics_service import (get_department_call_by_z_t,
                                        get_major_call_by_z_t,
                                        get_grade_call_by_z_t)

statistics = Blueprint("statistics", __name__, url_prefix="/statistics")


@statistics.route("/get_department_call", methods=('get', 'post'))
def get_department_call():
    data = request.json
    Z = data['z']
    T = data['t']
    LOG.debug(data)
    department_calls = get_department_call_by_z_t(Z, T)
    res = condition['200_1']
    res['data']= department_calls
    return dumps(res)


@statistics.route("/get_major_call", methods=('get', 'post'))
def get_major_call():
    data = request.json
    Z = data['z']
    T = data['t']
    d_id = data['d_id']
    LOG.debug("进入get_major_call")
    major_calls = get_major_call_by_z_t(Z, T, d_id)
    res = condition['200_1']
    res['data'] = major_calls
    return dumps(res)


@statistics.route("/get_grade_call", methods=('get', 'post'))
def get_grade_call():
    data = request.json
    Z = data['z']
    T = data['t']
    m_id = data['m_id']
    LOG.debug("进入get_grade_call")
    grade_calls = get_grade_call_by_z_t(Z, T, m_id)
    res = condition['200_1']
    res['data'] = grade_calls
    return dumps(res)